package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

public class StudentDao {
    // 创建一个长度为10的学生数组用于存放数据
    private static Student[] students = new Student[10];


    public boolean addStudent(Student stu) {
        // 开始假设数组已经存满
        int index = -1;
        // 遍历找到数组中为null的元素的位置
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null){
                // 如果找到，修改index的值
                index = i;
                break;
            }
        }
        if (index == -1){
            return false;
        } else {
            // 将学生对象添加到数组
            students[index] = stu;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return students;
    }

    public void remove(int id) {
        for (int i = 0; i < students.length; i++) {
            if (i == id) {
                students[i] = null;
            }
        }
    }

    public void updateStudent(int index, Student student) {
        students[index] = student;
    }
}
